<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("userFunction.php");

$errMsg = "";

$sSQL = "SELECT u.*, p.description, p.id_profile FROM user u " .
		" LEFT JOIN user_profile up ON up.id_user = u.id_user " .
		" LEFT JOIN profile p ON p.id_profile = up.id_profile " .
		" WHERE u.id_user= ".sxREQ('id');
$objSDAO->query($sSQL,"sel");
$row = $objSDAO->fetch("sel");
//aree dati abilitate
$sSQL = "SELECT MIN(dp.permission) AS perm, da.data_area, p.description FROM  data_area da " .
		" LEFT JOIN data_area_permission dp ON dp.data_area = da.data_area " .
		" LEFT JOIN user_profile up ON up.id_profile = dp.id_profile AND id_user = ".sxREQ('id') .
		" LEFT JOIN profile p ON p.id_profile = up.id_profile " .
		" GROUP BY da.data_area".
		" ORDER BY da.data_area, dp.permission DESC ";
$objSDAO->query($sSQL, "datare");

//flag attivati
$sSQL = "SELECT fp.* FROM  user_profile up " .
		" LEFT JOIN profile p ON p.id_profile = up.id_profile " .
		" LEFT JOIN flag_profile fp ON fp.id_profile = up.id_profile " .
		" WHERE up.id_user= ".sxREQ('id');
$objSDAO->query($sSQL, "flag");

if(sxREQ("reqtype") == "delete"){
	$errMsg .= userDel();	
}

//output HTML
$aOpz = array("tit2"=>_t("Visualizza utente"));
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
function confDelUser() {
	if (confirm("Confermi l'eliminreqtype dell'utente ?")) {
		document.location.href = "<?=$_SERVER['PHP_SELF'].'?id='.sxREQ('id').'&reqtype=delete&xsn='.XSN?>";
	}
}
</script> 
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//output comandi
$aCmd = array(
	"prolis"=>array("pag"=>"../prof/profileList.php",
						"title"=>_t("lista profili"),
						"text"=>_t("profili"),
						"perm"=>"visprof", "align"=>"L"),
	"pro"=>array("pag"=>"userProfileIns.php?id=".sxREQ('id'),
						"title"=>_t("gestisce i profili utente"),
						"text"=>_t("profili utente"),
						"perm"=>"upduser"),
	"del"=>array("pag"=>"javascript:confDelUser()",
						"title"=>_t("elimina l'utente"),
						"text"=>_t("elimina"),
						"perm"=>"upduser"),
	"mod"=>array("pag"=>"userMod.php?id=".sxREQ('id'),
						"title"=>_t("Modifica i dati dell'utente"),
						"text"=>_t("modifica"),
						"perm"=>"upduser"),
	"back"=>array("pag"=>$objUser->getHistory(-1,false),
						"title"=>$objUser->getHistoryText(-1),
						"text"=>$objUser->getHistoryText(-1))
	);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">"._t("Utente")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">"._t("nome utente")."*</td>" .
			"<td class=\"text01\">".sxHE($row['uid'])."</td>" .
		"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("nome")."</td>" .
	"<td class=\"text01\">".sxHE($row['name'])."</td>" .
"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("cognome")."</td>" .
	"<td class=\"text01\">".sxHE($row['last_name'])."</td>" .
"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("autenticreqtype")."</td>" .
	"<td class=\"text01\">".sxHE($row['authenticaz'])."</td>" .
"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("telefono cellulare")."</td>" .
	"<td class=\"text01\">".sxHE($row['mobile_phone'])."</td>" .
"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("e-mail")."</td>" .
	"<td class=\"text01\">".sxHE($row['e_mail'])."</td>" .
"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("telefono fisso")."</td>" .
	"<td class=\"text01\">".sxHE($row['phone'])."</td>" .
"</tr>\n");	
echo("<tr>" .
	"<td class=\"labl01\">"._t("profili")."</td>" .
	"<td class=\"text01\">");	
$pos=0;
$sep='';
while($row = $objSDAO->fetch("sel",$pos)){
	$pos='';
	$href = SERVER_URL_ROOT."/app/uspr/prof/profileView.php?id=".$row["id_profile"]."&xsn=".XSN;
	echo $sep."<a href='$href' class='text01'>".sxHE($row["description"])."</a>";
	$sep = ', ';
}
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("aree dati")."</td>" .
	"<td class=\"text01\">");	
$pos=0;
$sep='';
while($row = $objSDAO->fetch("datare",$pos)){
	$pos='';
	if ($row["perm"]=='N') continue;
	echo $sep.sxHE($row["data_area"])."(".(($row["perm"]=='M')?'modifica':'visualizza').")";
	$sep = ', ';
}
echo("</td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
$objSDAO->close();

function userDel(){
	global $objSDAO;
	$errMsg = "";
	$sSQL = "DELETE FROM user " .
			" WHERE id_user=".sxREQUEST('id');	
	$objSDAO->exec($sSQL);
	$sSQL = "DELETE FROM user_profile " .
			" WHERE id_user=".sxREQUEST('id');	
	$objSDAO->exec($sSQL);
	logRecord("570", _t("Eliminato l'utente %s",sxREQUEST('id')));
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return $errMsg;
}


?>
